<?php

/**
 * Subclass for representing a row from the 'video' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Video extends BaseVideo
{

    public function getTitleLng()
    {
        //debugvar($this->news, 'news');
        $this->setCulture(sfContext::getInstance()->getUser()->getCulture());
        return $this->getTitle();
    }

    public function hydrate(ResultSet $rs, $startcol = 1)
    {
        $this->setCulture(sfContext::getInstance()->getUser()->getCulture());
        return parent::hydrate($rs, $startcol);
    }

    public function getStatusName()
    {
        return MyConstants::getStatusName($this->getStatusId());
    }

    public function getVideoSrc()
    {
        $src = $this->video_src;
        if( $src )
        {
            $mask = "/(width)([\ \t]*)(=)([\ \t]*)([\"|'])([0-9]+)/i";
            preg_match_all($mask, $src, $arMatches);
            //debugvar($arMatches);
            if ($this->getWidth()!= 0)
            {
                $src = preg_replace($mask, 'width="' . $this->getWidth(), $src);
            }
            $mask = "/(height)([\ \t]*)(=)([\ \t]*)([\"|'])([0-9]+)/i";
            if ($this->getHeight()!=0)
            {
                $src = preg_replace($mask, 'height="' . $this->getHeight(), $src);
            }
            //debugvar($src);
        }
        return $src;
    }
    
    public function getSwfSrc()
    {
    	$arMatches = array(2=>'');
        $src = $this->video_src;
        //$src = '<object width="1280" height="750"><param name="movie" value="https://www.youtube.com/v/hfLf4k7fNVU?fs=1&amp;hl=lt_LT&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="https://www.youtube.com/v/hfLf4k7fNVU?fs=1&amp;hl=lt_LT&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="1280" height="750"></embed></object>';
        if( $src )
        {
        	//$mask = "/(src)([\ \t]*)(=)([\ \t]*)([\"|'])([^\"]+)([\"|'])/i";
            $mask = "/(com\/v\/)([^\?]+)([\?])/i";
            preg_match($mask, $src, $arMatches);
            //debugvar($arMatches);
        }
        return $arMatches[2];
    }
    
    public function getPreviewImage()
    {
        $img = '';
        $mask = '/(src=\\")([^"]+)/';
        if( preg_match($mask, $this->video_src, $arMatches) )
        {
//            debugvar($arMatches);
            $sImg = strrchr($arMatches[2], '/');
//            debugvar('Without http: ' . $sImg);
            if( $pos = strpos($sImg, '&') )
            {
                $sImg = substr($sImg, 1, $pos - 1);
//                debugvar('Clean: ' . $sImg);
                $img = sprintf('http://i1.ytimg.com/vi/%s/default.jpg', $sImg);
            }            
        }

        return $img;
    }
}
